package com.sunda.spmsuser.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsuser.entity.SpmsPermission;
import com.sunda.spmsuser.mapper.SpmsPermissionMapper;
import com.sunda.spmsuser.service.ISpmsPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * spms权限表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-01-25
 */
@Service
public class SpmsPermissionServiceImpl extends ServiceImpl<SpmsPermissionMapper, SpmsPermission> implements ISpmsPermissionService {

    @Autowired
    SpmsPermissionMapper spmsPermissionMapper;

    @Override
    public List<SpmsPermission> getUserPerms(String userId) {
        try {
            return spmsPermissionMapper.getUserPerm(userId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> getAllPerm() {
        return spmsPermissionMapper.getAllPerm();
    }

    @Override
    public List<Map<String, Object>> getRolePerm() {
        return spmsPermissionMapper.getRolePerm();
    }

    @Override
    public List<Map<String, Object>> getPermsByUserId(String userId) {
        return spmsPermissionMapper.getPermsByUserId(userId);
    }

    /***
     * {
     * 	"redirect": null,
     * 	"permissionId": "200206",
     * 	"menuEnName": "Material search",
     * 	"icon": null,
     * 	"description": null,
     * 	"updateUser": "999947",
     * 	"menuName": "物料搜索",
     * 	"componentUrl": null,
     * 	"isLeaf": 0,
     * 	"parentId": null,
     * 	"url": "/search",
     * 	"sortNo": null,
     * 	"permsType": "0",
     * 	"menuType": null,
     * 	"perms": null,
     * 	"createUser": "",
     * 	"componentName": null,
     * 	"isRoute": 1,
     * 	"alwaysShow": null
     * }
     * @param doc
     * @return
     */
    @Override
    public ResponseResult createSpmsPerm(JSONObject doc) {
        if (!doc.containsKey("permissionId")){
            return ResponseResult.error("请求参数错误");
        }
        SpmsPermission spmsPermission = new SpmsPermission();
        spmsPermission.setPermissionId(doc.getString("permissionId"));
        spmsPermission.setParentId(doc.get("parentId") == null ? "" : doc.getString("parentId"));
        spmsPermission.setMenuName(doc.get("menuName") == null ? "" : doc.getString("menuName"));
        spmsPermission.setMenuEnName(doc.get("menuEnName") == null ? "" : doc.getString("menuEnName"));
        spmsPermission.setUrl(doc.get("url") == null ? "" : doc.getString("url"));
        spmsPermission.setComponentUrl(doc.get("componentUrl") == null ? "" : doc.getString("componentUrl"));
        spmsPermission.setComponentName(doc.get("componentName") == null ? "" : doc.getString("componentName"));
        spmsPermission.setRedirect(doc.get("redirect") == null ? "" : doc.getString("redirect"));
        spmsPermission.setMenuType(doc.get("menuType") == null ? 0 : Integer.parseInt(doc.getString("menuType")));
        spmsPermission.setPerms(doc.get("perms") == null ? "" : doc.getString("perms"));
        spmsPermission.setPermsType(doc.get("permsType") == null ? "" : doc.getString("permsType"));
        spmsPermission.setSortNo(doc.get("sortNo") == null ? 1000 : Integer.parseInt(doc.getString("sortNo")));
        spmsPermission.setAlwaysShow(doc.get("alwaysShow") == null ? 0 : Integer.parseInt(doc.getString("alwaysShow")));
        spmsPermission.setIcon(doc.get("icon") == null ? "" : doc.getString("icon"));
        spmsPermission.setIsLeaf(doc.get("isLeaf") == null ? 0 : Integer.parseInt(doc.getString("isLeaf")));
        spmsPermission.setDescription(doc.get("description") == null ? "" : doc.getString("description"));
        spmsPermission.setCreateUser(doc.get("createUser") == null ? "" : doc.getString("createUser"));
        spmsPermission.setUpdateUser(doc.get("updateUser") == null ? "" : doc.getString("updateUser"));
        /** 存在 permissionId 则更新数据 */
        if (spmsPermissionMapper.selectOne(new QueryWrapper<SpmsPermission>().eq("PERMISSION_ID", spmsPermission.getPermissionId())) != null){
            int resp = spmsPermissionMapper.update(spmsPermission, new QueryWrapper<SpmsPermission>().eq("PERMISSION_ID", spmsPermission.getPermissionId()));
            return ResponseResult.success("更新数据：" + resp);
        }
        /** 不存在 permissionId 则新建数据 */
        //spmsPermission.setPermissionId(UUID.randomUUID().toString().replaceAll("-","").toLowerCase());
        int res = spmsPermissionMapper.insert(spmsPermission);
        return ResponseResult.success("新建数据：" + res);
    }
}
